/**
 * ======================
 * TOKEN file
 * =======================
 *
 * The function  about token and wxcode is defined here
 */

import cookieApi from '@/utils/libs/cookieApi'
import Qs from 'qs'
const serviceType = process.env.NODE_ENV
const SSO_COOKIE_NAME = serviceType + 'ssoToken'
const TOKEN_KEY = serviceType + 'Authorization'
const WX_CODE = serviceType + 'WxCode'
const REFRESH_TOKEN_KEY = serviceType + 'RefreshToken'
const LOGIN_STAMP = serviceType + 'LoginStamp'
const USER_KEY = serviceType + 'UserKey'
const driver_Id = serviceType + 'DriverId'
const LOGIN_TYPE = serviceType + 'LoginType' // 司机dirver or 公司company 用于辨别是司机登录 还是公司登录
const ROUTER_LIST = serviceType + 'routerList'
const TENANT_ID = serviceType + 'TenantId' // 公司(承运商)标示
const ACCOUNT_ID = serviceType + 'AccountId' // 账户标示
const CAR_INFO = serviceType + 'CarInfo' // 当前车队信息
const ALL_CAR_INFO = serviceType + 'AllCarInfo' // 所有车队信息
const LISTINFO = serviceType + 'ListInfo'// 可竞价列表信息
const ROLE = 'Role'// 区分是否为全员竞价字段
const OPENID = 'Openid'// 微信openID
/**
 *   获取会话token ，token是2小时 失效
 *   如果session 没拿到就拿 local存储,如果local存储 也没有，
 *   就拿 cookie内存储的token, 如果cookie也被清除了 那么会话token为空需要重新登录
*/

/**
 * 此处 有个坑  Storage.saveToSession() 使用后 会使 键值带上 "",
 * 导致 此键值 不适用
* */


// 是否登录
export const isLogin = () => {
  return !!getToken()
}

export const getToken = () => {
  // 先从sessionStorage中获取token，不存在则从cookie取
  let token = sessionStorage.getItem(TOKEN_KEY)
  if (!token) {
    if (cookieApi.get(SSO_COOKIE_NAME)) {
      // 自定义token格式,加Bearer 做前缀
      token = 'Bearer ' + cookieApi.get(SSO_COOKIE_NAME)
      sessionStorage.setItem(TOKEN_KEY, token)
    } else {
      token = null
    }
  }
  return token
}
// 设置会话token
export const setToken = (token) => {
  if (token) {
    // 把token存入sessionStorage和cookie，以SSO_COOKIE_NAME为键名
    sessionStorage.setItem(TOKEN_KEY, 'Bearer ' + token)
    cookieApi.set(SSO_COOKIE_NAME, token)
  }
}
export const cleanToken = () => {
  sessionStorage.removeItem(TOKEN_KEY) // session-token
  sessionStorage.removeItem(REFRESH_TOKEN_KEY) // local-refreshToken
  cookieApi.delete(REFRESH_TOKEN_KEY) // cookie-refreshToken
  cookieApi.delete(SSO_COOKIE_NAME) // cookie-token
}


// 是否有微信授权
export const isWxAuthorize = () => {
  return !!getWxCode()
}
// 获取会话wxcode
export const getWxCode = () => {
  let wxcode = sessionStorage.getItem(WX_CODE)
  if (!wxcode) {
    wxcode = null
  }
  return wxcode
}
// 设置会话wxcode
export const setWxCode = (wxcode) => {
  if (wxcode) {
    sessionStorage.setItem(WX_CODE, wxcode)
  }
}
export const cleanWxCode = () => {
  sessionStorage.removeItem(WX_CODE)
}


// 是否有 refreshToken
export const isRefreshLogin = () => {
  return !!getRefreshToken()
}
// 获取会话RefreshToken
export const getRefreshToken = () => {
  // let refreshToken = sessionStorage.getItem(REFRESH_TOKEN_KEY)
  // if (!refreshToken) {
  //   refreshToken = null
  // }
  let refreshToken = sessionStorage.getItem(REFRESH_TOKEN_KEY)
  if (!refreshToken) {
    if (cookieApi.get(REFRESH_TOKEN_KEY)) {
      refreshToken = cookieApi.get(REFRESH_TOKEN_KEY)
      console.log('getRefreshToken-cookie')
    } else {
      refreshToken = null
      console.log('getRefreshToken-rToken为空')
    }
  }
  return refreshToken
}
// 设置 local cookie  存储 RefreshToken
export const setRefreshToken = (refreshToken) => {
  if (refreshToken) {
    sessionStorage.setItem(REFRESH_TOKEN_KEY, refreshToken)
    cookieApi.set(REFRESH_TOKEN_KEY, refreshToken)
  }
}
export const cleanRefreshToken = () => {
  // local-refreshToken
  sessionStorage.removeItem(REFRESH_TOKEN_KEY)
  cookieApi.delete(REFRESH_TOKEN_KEY)
}


// 是否有 loginStamp LoginStamp
export const isLoginStamp = () => {
  return !!getLoginStamp()
}
// 获取会话 loginStamp
export const getLoginStamp = () => {
  let loginStamp = sessionStorage.getItem(LOGIN_STAMP)
  // console.log('getLoginStamp-session', loginStamp)
  if (!loginStamp) {
    if (cookieApi.get(LOGIN_STAMP)) {
      loginStamp = cookieApi.get(LOGIN_STAMP)
      // console.log('getLoginStamp-cookie', loginStamp)
    } else {
      loginStamp = null
      // console.log('getLoginStamp-loginstamp为空', loginStamp)
    }
  }
  return loginStamp
}
// 设置会话 loginStamp
export const setLoginStamp = (loginStamp) => {
  if (loginStamp) {
    sessionStorage.setItem(LOGIN_STAMP, loginStamp)
    cookieApi.set(LOGIN_STAMP, loginStamp)
  }
}
export const cleanLoginStamp = () => {
  sessionStorage.removeItem(LOGIN_STAMP) // session-loginStamp
  cookieApi.delete(LOGIN_STAMP) // cookie-loginStamp
}


// 是否有 userkey Userkey USER_KEY
export const isHaveUserkey = () => {
  return !!getUserkey()
}
// 获取会话 userkey
export const getUserkey = () => {
  let userkey = sessionStorage.getItem(USER_KEY)
  if (!userkey) {
    if (cookieApi.get(USER_KEY)) {
      userkey = cookieApi.get(USER_KEY)
    } else {
      userkey = null
    }
  }
  return userkey
}
// 设置会话 userkey
export const setUserkey = (userkey) => {
  if (userkey) {
    sessionStorage.setItem(USER_KEY, userkey)
    cookieApi.set(USER_KEY, userkey)
  }
}
export const cleanUserkey = () => {
  sessionStorage.removeItem(USER_KEY)
  cookieApi.delete(USER_KEY) // cookie-userkey
}



// 是否有 DriverId DriverId driver_Id
export const isHaveDriverId = () => {
  return !!getDriverId()
}
// 获取会话 DriverId
export const getDriverId = () => {
  let DriverId = sessionStorage.getItem(driver_Id)
  if (!DriverId) {
    if (localStorage.getItem(driver_Id)) {
      DriverId = localStorage.getItem(driver_Id)
    } else {
      DriverId = null
    }
  }
  return DriverId
}
// 设置会话 DriverId
export const setDriverId = (DriverId) => {
  if (DriverId) {
    sessionStorage.setItem(driver_Id, DriverId)
    localStorage.setItem(driver_Id, DriverId)
  }
}
export const cleanDriverId = () => {
  sessionStorage.removeItem(driver_Id)
  localStorage.removeItem(driver_Id)
}



// 是否有 const LOGIN_TYPE = 'LoginType' logintype
export const isHaveLoginType = () => {
  return !!getLoginType()
}
// 获取会话 logintype
export const getLoginType = () => {
  let logintype = sessionStorage.getItem(LOGIN_TYPE)
  // console.log('getLoginType-session', logintype)
  if (!logintype) {
    if (cookieApi.get(LOGIN_TYPE)) {
      logintype = cookieApi.get(LOGIN_TYPE)
      // console.log('getLoginType-cookie', logintype)
    } else {
      logintype = null
      // console.log('getLoginType-logintype为空', logintype)
    }
  }
  return logintype
}
// 设置会话 logintype
export const setLoginType = (logintype) => {
  if (logintype) {
    sessionStorage.setItem(LOGIN_TYPE, logintype)
    cookieApi.set(LOGIN_TYPE, logintype)
  }
}
export const cleanLoginType = () => {
  sessionStorage.removeItem(LOGIN_TYPE)
  cookieApi.delete(LOGIN_TYPE) // cookie-userkey
}


// 是否有 const ROUTER_LIST = 'routerList'  RouterList
export const isRouterList = () => {
  return !!getRouterList()
}
// 获取会话 routerList
export const getRouterList = () => {
  let now_routerList = Qs.parse(sessionStorage.getItem(ROUTER_LIST))
  let routerList = []
  if (now_routerList) {
    for (const k in now_routerList) {
      routerList.push(now_routerList[k])
    }
  }
  // console.log('getRouterList-session')
  if (!now_routerList) {
    if (cookieApi.get(ROUTER_LIST)) {
      now_routerList = Qs.parse(cookieApi.get(ROUTER_LIST))
      if (now_routerList) {
        for (const k in now_routerList) {
          routerList.push(now_routerList[k])
        }
      }
      console.log('getRouterList-cookie', routerList)
    } else {
      routerList = null
      console.log('getRouterList-routerList 为空')
    }
  }
  return routerList
}
// 设置会话 logintype
export const setRouterList = (routerList) => {
  if (routerList) {
    sessionStorage.setItem(ROUTER_LIST, routerList)
    cookieApi.set(ROUTER_LIST, routerList)
  }
}
export const cleanRouterList = () => {
  sessionStorage.removeItem(ROUTER_LIST)
  cookieApi.delete(ROUTER_LIST) // cookie-routerList
}

// 是否有 const TENANT_ID = 'TenantId'  tenantid
export const isTenantId = () => {
  return !!getTenantId()
}
// 获取会话 tenantid
export const getTenantId = () => {
  let tenantid = sessionStorage.getItem(TENANT_ID)
  // console.log('getTenantId-session', tenantid)
  if (!tenantid) {
    if (cookieApi.get(TENANT_ID)) {
      tenantid = cookieApi.get(TENANT_ID)
      // console.log('getTenantId-cookie', tenantid)
    } else {
      tenantid = null
      // console.log('getTenantId-tenantId为空', tenantid)
    }
  }
  return tenantid
}
// 设置会话 tenantid
export const setTenantId = (tenantid) => {
  if (tenantid) {
    sessionStorage.setItem(TENANT_ID, tenantid)
    cookieApi.set(TENANT_ID, tenantid)
  }
}
export const cleanTenantId = () => {
  sessionStorage.removeItem(TENANT_ID)
  cookieApi.delete(TENANT_ID) // cookie
}


// const ACCOUNT_ID = serviceType + 'AccountId' accountId
export const isAccountId = () => {
  return !!getAccountId()
}
// 获取会话 accountId
export const getAccountId = () => {
  let accountId = sessionStorage.getItem(ACCOUNT_ID)
  // console.log('getaccountId-session', accountId)
  if (!accountId) {
    if (cookieApi.get(ACCOUNT_ID)) {
      accountId = cookieApi.get(ACCOUNT_ID)
      // console.log('getaccountId-cookie', accountId)
    } else {
      accountId = null
      // console.log('getaccountId-accountId为空', accountId)
    }
  }
  return accountId
}
// 设置会话 accountId
export const setAccountId = (accountId) => {
  if (accountId) {
    sessionStorage.setItem(ACCOUNT_ID, accountId)
    cookieApi.set(ACCOUNT_ID, accountId)
  }
}
export const cleanAccountId = () => {
  sessionStorage.removeItem(ACCOUNT_ID)
  cookieApi.delete(ACCOUNT_ID) // cookie
}


// 当前车队信息：carInfo以CAR_INFO为键名保存，保存到cookie和sessionStorage
export const isCarInfo = () => {
  return !!getCarInfo()
}
// 获取会话 carInfo
export const getCarInfo = () => {
  let carInfo = JSON.parse(sessionStorage.getItem(CAR_INFO))
  if (!carInfo) {
    if (cookieApi.get(CAR_INFO)) {
      carInfo = JSON.parse(cookieApi.get(CAR_INFO))
    } else {
      carInfo = null
    }
  }
  return carInfo
}
// 设置会话 carInfo
export const setCarInfo = (carInfo) => {
  if (carInfo) {
    sessionStorage.setItem(CAR_INFO, JSON.stringify(carInfo))
    cookieApi.set(CAR_INFO, JSON.stringify(carInfo))
  }
}
export const cleanCarInfo = () => {
  sessionStorage.removeItem(CAR_INFO)
  cookieApi.delete(CAR_INFO)
}

// 全部车队信息：allCarInfo以ALL_CAR_INFO为键名保存，保存到cookie和sessionStorage
export const isAllCarInfo = () => {
  return !!getAllCarInfo()
}
// 获取会话 allCarInfo
export const getAllCarInfo = () => {
  let allCarInfo = JSON.parse(sessionStorage.getItem(ALL_CAR_INFO))
  if (!allCarInfo) {
    if (cookieApi.get(ALL_CAR_INFO)) {
      allCarInfo = JSON.parse(cookieApi.get(ALL_CAR_INFO))
    } else {
      allCarInfo = null
    }
  }
  return allCarInfo
}
// 设置会话 allCarInfo
export const setAllCarInfo = (allCarInfo) => {
  if (allCarInfo) {
    sessionStorage.setItem(ALL_CAR_INFO, JSON.stringify(allCarInfo))
    cookieApi.set(ALL_CAR_INFO, JSON.stringify(allCarInfo))
  }
}
export const cleanAllCarInfo = () => {
  sessionStorage.removeItem(ALL_CAR_INFO)
  cookieApi.delete(ALL_CAR_INFO)
}

// 可竞价信息,跳转至可竞价的竞价明细需要使用，只存sessionStorage
export const isDealListInfo = () => {
  return !!getDealListInfo()
}
// 获取可竞价信息
export const getDealListInfo = () => {
  let dealList = JSON.parse(sessionStorage.getItem(LISTINFO))
  if (!dealList) {
    dealList = null
  }
  return dealList
}
// 设置可竞价信息
export const setDealListInfo = (dealListInfo) => {
  if (dealListInfo) {
    sessionStorage.setItem(LISTINFO, JSON.stringify(dealListInfo))
  }
}
export const cleanDealListInfo = () => {
  sessionStorage.removeItem(LISTINFO)
}


// 是否为竞价的区分字段：roles_val只存sessionStorage
export const isRolesVal = () => {
  return !!getRolesVal()
}
// 获取可竞价信息
export const getRolesVal = () => {
  let rolesVal = sessionStorage.getItem(ROLE)
  if (!rolesVal) {
    rolesVal = null
  }
  return rolesVal
}
// 设置可竞价信息
export const setRolesVal = (roleVal) => {
  if (roleVal) {
    sessionStorage.setItem(ROLE, roleVal)
  }
}
export const cleanRolesVal = () => {
  sessionStorage.removeItem(ROLE)
}

// 存openId
export const isOpenid = () => {
  return !!getOpenid()
}
// 获取可竞价信息
export const getOpenid = () => {
  let openid = sessionStorage.getItem(OPENID)
  if (!openid) {
    openid = null
  }
  return openid
}
// 设置可竞价信息
export const setOpenid = (openid) => {
  if (openid) {
    sessionStorage.setItem(OPENID, openid)
  }
}
export const cleanOpenid = () => {
  sessionStorage.removeItem(OPENID)
}
